package com.Work;

import com.alibaba.fastjson2.JSON;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

public class Exam02 {
    public static void main(String[] args) {
        File file = new File("days9.15/src/txtpack/game.txt");
        try (FileWriter writer = new FileWriter(file);
             FileReader reader = new FileReader(file)
        ) {
            Game game = new Game("永劫无间", 3.8, 45000, 98);
            Game game1 = new Game("CS:GO", 4.0, 5000, 198);
            String jsonString = JSON.toJSONString(game);
            String jsonString1 = JSON.toJSONString(game1);
            System.out.println(jsonString);
            // 实现了深拷贝
            Game game2 = JSON.parseObject(jsonString, Game.class);
            System.out.println(game2.getName() == game.getName()); // false
            System.out.println(game2.getName().equals(game.getName())); // true
            writer.write(jsonString);
            writer.write(jsonString1);
            writer.flush();
            char[] chars = new char[1024];
            int size;
            while ((size = reader.read()) != -1) {
                reader.read(chars,0,size);
            }
            System.out.println(chars);

        } catch (IOException e) {
            System.out.println(e.getMessage());
        }
    }
}

@Data
@AllArgsConstructor
@NoArgsConstructor
 class Game {
    private String name;
    private double star;
    private int size;
    private double money;
}
